package com.moyq5.permit.center.core.dao.impl;


import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import com.moyq5.mvc.framework.dao.BasicDao;
import com.moyq5.mvc.framework.dao.ExampleQuery;
import com.moyq5.permit.center.bean.entity.SysPermit;
import com.moyq5.permit.center.bean.entity.SysPermit_;
import com.moyq5.permit.center.bean.search.SysPermit4Search;
import com.moyq5.permit.center.core.dao.SysPermitDao;


/**
 * @author Moyq5's MvcMaker Tools
 */
@Repository
public class SysPermitDaoImpl extends BasicDao<SysPermit, SysPermit4Search> implements SysPermitDao {

	public SysPermitDaoImpl() {
		super(SysPermit.class);
	}

	@Override
	protected CriteriaQuery<SysPermit> createSearchCriteria(ExampleQuery<SysPermit> eq, SysPermit4Search t4s) {
		CriteriaQuery<SysPermit> cq = eq.getCriteriaQuery();
		
		@SuppressWarnings("unchecked")
		Root<SysPermit> cr = (Root<SysPermit>) cq.getRoots().iterator().next();
		List<Predicate> ps = new ArrayList<Predicate>();
		ps.add(cq.getRestriction());
		
		CriteriaBuilder cb = em().getCriteriaBuilder();
		
		if (null != t4s.getPermitIdIn()) {
			ps.add(cr.get(SysPermit_.permitId).in(t4s.getPermitIdIn()));
		}
		if (null != t4s.getTagGt()) {
			ps.add(cb.greaterThan(cr.get(SysPermit_.tag), t4s.getTagGt()));
		}
		// TODO 在这里实现其它属性（条件）的查询
		
		cq.where(ps.toArray(new Predicate[ps.size()]));
		
		return cq;
	}

}

